package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wanmao.test.bean.RYRWBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class RYRWService {
    private DBOpenHelper dBOpenHelper;

    public RYRWService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(RYRWBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("RYRW_TABLE", null, settingValue(bean));
    }
    private ContentValues settingValue(RYRWBean bean) {
        ContentValues values = new ContentValues();
        values.put("OBJ_ID", bean.getOBJ_ID());
        values.put("RYID", bean.getRYID());
        values.put("RWID", bean.getRWID());
        return values;
    }
    public List<RYRWBean> findALLRW(String RYID) {
        String sql = "select * from RYRW_TABLE where RYID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{RYID});

        return toListBean(cursor);

    }

    private List<RYRWBean> toListBean(Cursor cursor) {
        List<RYRWBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            RYRWBean bean = new RYRWBean();
            bean.setOBJ_ID(cursor.getString(cursor.getColumnIndex("OBJ_ID")));
            bean.setRWID(cursor.getString(cursor.getColumnIndex("RWID")));
            bean.setRYID(cursor.getString(cursor.getColumnIndex("RYID")));
            list.add(bean);
        }
        return list;
    }
}
